import { observable, action, computed, toJS } from "mobx";
import * as Apis from "../../apis/docExchangeApi";
import { ListViewPage, Browser } from "weaver-mobile-page";
import { Bubble, tools } from 'weaver-mobile';

const { getLabel } = tools;
class DocExchangeStore {

  @observable selectedTab = "0"; //选中的tab
  @observable selectedItem = [];
  @observable list = new ListViewPage.Store({ route: '/docExchange' });
  @observable tabs = [];
  @observable number = 0;//展示的tab页
  @observable loading = false;
  @observable hasRight = true;

  @observable exchangeType = 0;
  @observable visible = false;
  @observable showCheck = false;
  @observable selectedRowKeys = [];
  @observable selectedRequestids = [];
  @observable btnType = '';
  @observable currentId = '';//当前滑动按钮点击的公文id
  @observable documentIdentifier = "";//公文标识
  @observable currentName = '';//当前滑动按钮点击的公文标题

  // 作废、拒收、退回理由框参数
  @observable modalVisible = false;
  @observable textareaValue = '';
  @observable modalTile = '';
  @observable modalBtn = '';

  // 收发文单位浏览按钮
  @observable sendOtherBrowser = new Browser.Store({ route: '/docExchange', id: 'sendToOther' });


  //查看收文情况页面参数
  @observable checkReceiveList = new ListViewPage.Store({ route: '/docExchange', id: 'checkReceiveDoc' });
  @observable showDetailCheck = false;
  @observable selectedDetailRowKeys = [];
  @observable selectDepartids = [];
  @observable backId = '';


  @action
  setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
  }

  @computed get isSelectedAll() {
    return (
      this.selectedRowKeys.length >=
      (this.list.data.length !== 0 && this.list.data.map(d => d.id).length)
    );
  }

  @computed get isSelectedDetailAll() {
    return (
      this.selectedDetailRowKeys.length >=
      (this.checkReceiveList.data.length !== 0 && this.checkReceiveList.data.filter((item, i) => {
        return item.doc_status != '4';
      }).map(d => d.id).length)
    );
  }

  //获取数据表格
  @action.bound
  getList(formDatas = {}, isAnvanced = false) {
    this.loading = true;
    this.hasRight = true;
    const params = {
      isInnerChange: 1,
      state: this.selectedTab,
      ...formDatas,
    }
    this.list = new ListViewPage.Store({ route: '/docExchange' });
    Apis.getList(params, this.exchangeType).then(data => {
      if (data.sessionkey) {
        this.list.getData({ dataKey: data.sessionkey, current: 1 }, () => {
          this.loading = false;
        }, true);
      } else {
        this.hasRight = false;
      }
    })
  }

  //获取tabs
  @action.bound
  getTabs() {
    const params = {
      menuType: this.exchangeType == 1 ? 'receiveDoc' : '',
      isInnerChange: 1
    }
    Apis.getTabs(params, this.exchangeType).then(data => {
      if (data.groupinfo) {
        this.tabs = this.changeTabs(data.groupinfo);
      }
    })
  }

  @action.bound
  changeTypeTab(tab) {
    this.exchangeType = tab.key;
    this.selectedTab = '0';
    this.number = '0';
    this.selectedRowKeys = [];
    this.showCheck = false;
    this.getTabs();
    this.getList();
  }

  @action.bound
  tabClick(tab) {
    this.selectedTab = tab.viewcondition;
    this.selectedRowKeys = [];
    this.showCheck = false;
    this.getList();
    this.changeTabs(this.tabs);
  }

  //数据格式不匹配 为了符合组件转换数据格式
  @action.bound
  changeTabs(tabs) {
    tabs.map((item, index) => {
      if (item.viewcondition === this.selectedTab) {
        this.number = index;
      }
    })
    return tabs;
  }

  @action
  selectALL = () => {
    this.selectedRowKeys = this.list.data.map(d => d.id);
    this.list.selectAll();
    this.list.updateDataSource();
  }
  @action
  selectNone = () => {
    this.selectedRowKeys = [];
    this.list.selectNone();
    this.list.updateDataSource();
  }

  @action
  showListCheck = () => {
    this.list.haveCheck = true;
  }

  @action
  receiveDoc = () => {
    Apis.receiveDoc().then(res => {
      if (res.api_status) {
        Bubble.success(res.api_message);
        this.getList();
      }
    })
  }

  @action.bound
  initData() {
    this.number = 0;
    this.selectedItem = [];
    this.showCheck = false;
    this.selectedRowKeys = [];
    this.formData = {
      odocSubject: "",
      odoctype: "",
      topictype: "",
      odocTopicType: "",
      dispatchnum: "",
      writtendateselect: "0",
      readstatus: "",
    }
  }


  //发送公文

  @action('发送公文')
  sendDoc = (id) => {
    const ids = this.selectedRowKeys.join(",");
    const params = {
      ids: id || ids,
      status: 1,
      isInnerChange: 1
    }
    Apis.sendDoc(params).then(res => {
      if (res.api_status) {
        Bubble.success(res.api_message);
        this.getList();
        this.selectedRowKeys = [];
        this.showCheck = false;
      }
    })
  }

  @action
  setSelectedRequestids = (c) => {
    this.selectedRequestids = [];
    for (let row of c) {
      this.selectedRequestids.push(row.requestid);
    }
  }

  //发送其他单位
  @action.bound
  sendOtherUnits(unitIds, historyProp) {
    if (unitIds == '') {
      return;
    }
    this.loading = true;
    const ids = this.currentId || this.selectedRequestids.join(",");
    const params = {
      ids: ids,
      status: "0",
      receiverDepartments: unitIds,
      isInnerChange: 1
    }
    Apis.sendOtherUnits(params).then(data => {
      this.loading = false;
      if (data.api_status) {
        Bubble.success(getLabel("30700", "操作成功"));
        this.selectedRowKeys = [];
        this.selectedRequestids = [];
        this.currentId = '';
        historyProp.push(this.list.path);
        this.showCheck = false;
        this.getList();
        this.sendOtherBrowser = new Browser.Store({ route: '/docExchange', id: 'sendToOther' });
      } else {
        Bubble.error(data.api_message);
      }
    })
  }

  @action
  setBackDepart = (c) => {
    this.selectDepartids = [];
    for (let row of c) {
      this.selectDepartids.push(row.receive_company_id);
    }
  }

  //拒收、作废、退回、撤销
  @action
  reasonCheck = () => {
    if (this.textareaValue == '') {
      return Bubble.error(getLabel('', '有必填项未填写！'));
    } else {
      this.loading = true;
      const ids = this.currentId || this.selectedRowKeys.join(",");
      let params = {
        ids: ids,
        detail: this.textareaValue,
        status: 5,
        isInnerChange: 1,
      }
      if (this.btnType == 'back') {//撤销
        params.ids = this.backId;
        params.status = '4';
        params.receiverDepartments = this.selectDepartids.join(',');//单位id
      }
      if (this.btnType == 'batchRejection' || this.btnType == 'batchSendBack') {//撤销
        params.status = 3;
      }
      Apis.cancelledDoc(params, this.btnType).then(res => {
        this.loading = false;
        this.modalVisible = false;
        this.textareaValue = '';
        if (res.api_status) {
          Bubble.success(res.api_message || getLabel('', '操作成功'));
          if (this.btnType == 'back') {
            this.showDetailCheck = false;
            this.selectDetailNone();
            this.getSendDocinfo();
          } else {
            this.selectedRowKeys = [];
            this.currentId = '';
            this.btnType = '';
            this.showCheck = false;
            this.getList();
          }
        } else {
          Bubble.error(res.api_message);
        }
      })
    }
  }
  //获取查看收文单位信息的table数据
  @action.bound
  getSendDocinfo = (entityName = "") => {
    const params = {
      documentId: this.documentIdentifier,//公文标识
      entityName: entityName,//搜索参数  单位名称
    }
    this.loading = true;
    Apis.getSendDocinfo(params).then(data => {
      if (data.sessionkey) {
        this.checkReceiveList.getData({ dataKey: data.sessionkey, current: 1 }, () => {
          this.loading = false;
        }, true);
      }
    })
  }
  //查看收发文详情
  @action
  selectDetailALL = () => {
    let list = this.checkReceiveList.data.filter((item, i) => {
      return item.doc_status != '4';
    })
    this.selectedDetailRowKeys = list.map(d => d.id);
    this.checkReceiveList.selectAll();
    this.selectDepartids = [];
    for (let row of list) {
      this.selectDepartids.push(row.receive_company_id);
    }
    this.checkReceiveList.updateDataSource();
  }

  @action
  selectDetailNone = () => {
    this.selectedDetailRowKeys = [];
    this.checkReceiveList.selectNone();
    this.checkReceiveList.updateDataSource();
  }
  @action('离开浏览按钮页面需初始化参数')
  initCheckData = () => {
    this.selectDetailNone();
    this.currentId = '';
    this.currentName = '';
    this.showDetailCheck = false;
  }


  // ///////////////////////接收公文
  //签收
  @action.bound
  onSigninDoc(id = '') {
    this.loading = true;
    const params = {
      ids: id || this.selectedRowKeys.join(","),
      status: 1,
      isInnerChange: 1
    };
    Apis.signinDoc(params).then(data => {
      if (data.api_status) {
        Bubble.success(data.api_message);
      } else {
        Bubble.error(data.api_message);
      }
      this.selectedRowKeys = [];
      this.currentId = '';
      this.btnType = '';
      this.showCheck = false;
      this.getList(data);
    })
  }

}

export default DocExchangeStore;
